/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.teaching.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.teaching.bo.CourseChapterSubBankBo;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.teaching.entity.CourseChapterSubBankEntity;
import org.springblade.modules.teaching.vo.CourseChapterSubBankVO;
import org.springblade.modules.teaching.wrapper.CourseChapterSubBankWrapper;
import org.springblade.modules.teaching.service.ICourseChapterSubBankService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Arrays;
import java.util.List;

/**
 * 课程小节与题库关联表 控制器
 *
 * @author
 * @since 2023-03-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-courseChapterSubBank/courseChapterSubBank")
@Api(value = "课程小节与题库关联表", tags = "课程小节与题库关联表接口")
public class CourseChapterSubBankController extends BladeController {

	private final ICourseChapterSubBankService courseChapterSubBankService;
	private final CommonUtil commonUtil;
	/**
	 * 课程小节与题库关联表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入courseChapterSubBank")
	public R<CourseChapterSubBankVO> detail(CourseChapterSubBankEntity courseChapterSubBank) {
		CourseChapterSubBankEntity detail = courseChapterSubBankService.getOne(Condition.getQueryWrapper(courseChapterSubBank));
		return R.data(CourseChapterSubBankWrapper.build().entityVO(detail));
	}


	/**
	 * 课程小节与题库关联表 自定义分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入courseChapterSubBank")
	public R<IPage<CourseChapterSubBankVO>> page(CourseChapterSubBankVO courseChapterSubBank, Query query) {
//		if (!commonUtil.checkRoleAlias(TrainingRoleConstant.Administrator)) courseChapterSubBank.setCreateUser(AuthUtil.getUserId());
		commonUtil.checkRoleAliasPermission(courseChapterSubBank,courseChapterSubBank.getClass());
		IPage<CourseChapterSubBankVO> pages = courseChapterSubBankService.selectCourseChapterSubBankPage(Condition.getPage(query), courseChapterSubBank);
		return R.data(pages);
	}

	/**
	 * 课程小节与题库关联表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入courseChapterSubBank")
	public R save(@Valid @RequestBody CourseChapterSubBankBo courseChapterSubBank) {
		if (StringUtils.isBlank(courseChapterSubBank.getSubId())) throw new ServiceException("请选择要绑定课程的小节");
		String[] connectIds = StringUtils.splitByWholeSeparatorPreserveAllTokens(courseChapterSubBank.getConnnectId(),",");
		String subId=courseChapterSubBank.getSubId();
		Integer sort=courseChapterSubBank.getSort();

		for (String connectId:connectIds){
			CourseChapterSubBankEntity itemEntity=new CourseChapterSubBankEntity();
			itemEntity.setConnnectId(Long.valueOf(connectId));
			itemEntity.setSubId(Long.valueOf(subId));
			if (null==sort) throw new ServiceException("排序不能为空");
			itemEntity.setSort(sort++);
			courseChapterSubBankService.save(itemEntity);
		}
		return R.success("操作成功");
	}

	/**
	 * 课程小节与题库关联表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入courseChapterSubBank")
	public R update(@Valid @RequestBody CourseChapterSubBankEntity courseChapterSubBank) {
		return R.status(courseChapterSubBankService.updateById(courseChapterSubBank));
	}


	/**
	 * 课程小节与题库关联表 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(courseChapterSubBankService.deleteLogic(Func.toLongList(ids)));
	}


}
